import Gender
class SearchWithoutLogonController {
    static allowedMethods = [search:['POST']]

    def reset = {
        flash.message = null
        redirect(url:"${resource(dir:'')}")
    }

    def search = {
        log.debug params

        def mybook = params.mybook? params.mybook.trim() : null

        if(!mybook) {
            log.debug 'user did not input an owned book'
            flash.message = message(code:'error.no_book_input')
            redirect(url:"${resource(dir:'')}", params:[error:message(code:'error.no_book_input')])
            return
        }else {
            session["mybook"] = mybook
            log.debug "user inputs a book '$mybook' "
        }

        def mycity = params.mycity? params.mycity.trim() : null
        
        if(!mycity) {
            log.debug 'user did not input his or her city'
            flash.message = message(code:'error.no_city_input')
            redirect(url:"${resource(dir:'')}")
            return
        }else {
            def city = City.findById(mycity)
            if(!city) {
                log.debug 'cannot find the city'
                flash.message = message(code:'error.no_city_input')
                redirect(url:"${resource(dir:'')}")
                return
            } else {
                session["mycity"] = mycity
                log.debug "user selects the city '$city'"
            }
        }

        def targetGender = 'A'
        
        if(params.targetGender && ['F', 'M', 'A'].contains(params.targetGender)) {
            targetGender = params.targetGender
        }
        session["targetGender"] = targetGender

        log.debug "user's interested in $targetGender gender"
            
        def criteria = BookInstance.createCriteria()
        def results
        if(targetGender == 'A') {
            results = criteria {
                owner {
                    and {
                        city {
                            eq('id', mycity as long)
                        }
                        wantedBooks{
                            ilike('name', mybook)
                        }
                    }
                }
            }
        } else {
            results = criteria {
                owner {
                    and {
                        eq('gender', targetGender)
                        city {
                            eq('id', mycity as long)
                        }
                        wantedBooks{
                            ilike('name', mybook)
                        }
                    }
                }
            }
        }

        def owners = results.owner.unique()
        def books = results.profile.unique()

        log.debug "found ${books.size()} book profiles to be exchanged"
        log.debug books
        def resultMessage

        if(books.size()) {
            def messageCode
            switch(targetGender) {
                    case 'F': messageCode = 'label.found_exchange_books_by_female'; break
                    case 'M': messageCode = 'label.found_exchange_books_by_male'; break
                    default: messageCode = 'label.found_exchange_books_by_any'; break
            }
             
            resultMessage = message(code:messageCode, args:[owners.size(), mybook, books.size()])
        } else {
            resultMessage = message(code:'label.found_no_exchange_books')
        }
        
        [bookList: books, bookTotal:books.size(), resultMessage:resultMessage]
    }
}
